package org.csu.jpetstore_users_backend_pj.controller;

import org.csu.jpetstore_users_backend_pj.result.CommonResponse;
import org.csu.jpetstore_users_backend_pj.result.ResponseCode;
import org.csu.jpetstore_users_backend_pj.util.AliOssUtil;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@RestController
@CrossOrigin(origins = "http://localhost:5173")
public class FileUploadController {
    @PostMapping("/files")
    public CommonResponse upload(MultipartFile file) throws Exception {
        String originalFilename = file.getOriginalFilename();
        //保证文件名字唯一防止文件覆盖
        String filename = UUID.randomUUID().toString()+originalFilename.substring(originalFilename.lastIndexOf("."));
        String url=AliOssUtil.uploadFile(filename,file.getInputStream());

        return CommonResponse.createForSuccess(ResponseCode.GetHTTPUrlSuccess.getCode(),ResponseCode.GetHTTPUrlSuccess.getDescription(),url);
    }
}
